package com.xjtu.mybatisplus.common.config;

import com.xjtu.mybatisplus.interceptor.JwtTokenAdminInterceptor;
import com.xjtu.mybatisplus.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
//import org.springframework.security.config.annotation.web.configurers.DefaultLoginPageConfigurer;
//import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Value("${file-upload-path}")
    private String fileUploadPath;

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    @Override
    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/api/admin/**")
                .excludePathPatterns("/api/admin/login");
    }

//        registry.addInterceptor(jwtTokenUserInterceptor)
//                .addPathPatterns("api/user/**")
//                .excludePathPatterns("api/user/login")
//                .excludePathPatterns("api/auth/**");
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("进行跨域配置");
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600)
                .exposedHeaders("Authorization")
        ;
    }

//    @Bean
//    public WebSecurityConfigurerAdapter loginPageConfig(){
//        return new WebSecurityConfigurerAdapter() {
//            @Override
//            public void configure(HttpSecurity httpSecurity) throws Exception {
//                httpSecurity.removeConfigurer(DefaultLoginPageConfigurer.class); //将默认加载的登录页配置删除
//            }
//        };
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/files/**").addResourceLocations("file:" + fileUploadPath);
        super.addResourceHandlers(registry);
    }

//    @Bean
//    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
//        http.authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest()
//                        .permitAll())
//                .csrf(AbstractHttpConfigurer::disable);
//        return http.build();
//    }


}
